//Component Object
Component({
  pageLifetimes:{
    show:function(){
      this.startAutoPlay()
    },
    hide:function(){
      this.stopAutoplay()
    }
  },
  properties: {
    record:{
      type:Object,
      value:null,
    },
    firstSwiperPindex:{
      type:Number,
      value:0,
    },
    firstSwiperCurrentIndex:{
      type:Number,
      value:0,
      observer: function(newValue:number){
        let that = this as any
        if(newValue === this.data.firstSwiperPindex && !this.data.userStop){
          that.setData({
            autoplay:true
          })
        }
        // console.log('newValue:',newValue)
        // console.log('this.data.firstSwiperPindex:',this.data.firstSwiperPindex)
        if(Math.abs(newValue  - this.data.firstSwiperPindex) <= 3){
          // console.log('initContent')
          that.setData({
            initContent:true
          })
        }else {
          that.setData({
            initContent:false
          })
        }
        if(newValue !== this.data.firstSwiperPindex){
          this.stopAutoplay()
        }else {
          this.startAutoPlay()
        }
      }
    },
    showAudio:{
      type:Boolean,
      value:true,
    }
  },
  data: {
    childSwiperCurrentIndex:0,
    autoplay:false,
    showPlayBtn:false,
    userStop:false,
    initContent:false,
  },
  methods: {
    childSwiperChange(event:any){
      // console.log('childSwiperChange event:',event)
      this.setData({
        childSwiperCurrentIndex:event.detail.current
      })
      if(event.detail.source ==='touch'){
        this.stopAutoplay()
        this.setData({
          userStop:true
        })
      }
    },
    childSwiperTap(event:any){
      if(!this.data.showPlayBtn){
        this.setData({
          showPlayBtn:true,
          autoplay:false,
        })
      }
        
    },
    playChildSwiper(){
      this.setData({
        showPlayBtn:false,
        autoplay:true,
      })
    },
    stopAutoplay(){
      console.log('swiperContent stopAutoplay')
      this.setData({
        autoplay:false,
      })
    },
    resetSwiperCurrentIndex(){
      this.setData({
        childSwiperCurrentIndex:0,
      })
    },
    startAutoPlay(){
      console.log('swiperContent startAutoPlay')
      if(this.data.firstSwiperCurrentIndex === this.data.firstSwiperPindex && !this.data.userStop){
        this.setData({
          autoplay:true
        })
      }
      
    }
  },
  attached: function(){
    // console.log('swiperContent showAudio:',this.data.showAudio)
    // 只有滑到该轮播图时才自动播放
    if(this.data.firstSwiperCurrentIndex === this.data.firstSwiperPindex){
      this.setData({
        autoplay:true
      })
    }
    console.log('firstSwiperCurrentIndex:',this.data.firstSwiperCurrentIndex)
    console.log('firstSwiperPindex:',this.data.firstSwiperPindex)
    // 只有距当前页前3个页面，才初始化内容，避免内容过多小程序崩溃
    if(Math.floor(this.data.firstSwiperCurrentIndex  - this.data.firstSwiperPindex) <= 3){
      this.setData({
        initContent:true
      })
    }else {
      this.setData({
        initContent:false
      })
    }
    
    // console.log('this.data.firstSwiperCurrentIndex:',this.data.firstSwiperCurrentIndex)
    // console.log('this.data.firstSwiperPindex:',this.data.firstSwiperPindex)
    // console.log('this.data.autoplay:',this.data.autoplay)
  },

  detached: function(){
    this.stopAutoplay()
  },
});